// pages/mine/addCard/addCard.js

const App = getApp()

let { AJAX, timeTranfer,imgUrl } = require("../../../utils/util.js")
var interval = null //倒计时函数
Page({
   /**
    * 页面的初始数据 
    */
   data: { 
      navH: 0,
      cardList: {id:0,name:'请选择'},
      index: 0,  
      cardVal:'',    
      codeVal:'',
      time: '获取验证码', //倒计时 
      currentTime: 60,//限制60s
      isClick: false,//获取验证码按钮，默认允许点击  
      cardName:'', // 用户名字
      check_key:'', // 验证吗验证标识      
      reviseCard: true,
      bank_name:'',
      is_get_code:true,
     imgUrl: imgUrl
   },

   /**
    * 生命周期函数--监听页面加载
    */
   onLoad: function(options) {
      this.setData({
         navH: App.globalData.navHeight
      })
      let vm = this 
      vm.getBankList()
      vm.getPhone()
      vm.revise()
   },

   /**
    * 生命周期函数--监听页面初次渲染完成
    */
   onReady: function() {

   },

   /**
    * 生命周期函数--监听页面显示
    */
   onShow: function() {
      
   },

   /**
    * 生命周期函数--监听页面隐藏
    */
   onHide: function() {

   },

   /**
    * 生命周期函数--监听页面卸载
    */
   onUnload: function() {
     
   },

   /**
    * 页面相关事件处理函数--监听用户下拉动作
    */
   onPullDownRefresh: function() {

   },

   /**
    * 页面上拉触底事件的处理函数
    */
   onReachBottom: function() {

   },

   /**
    * 用户点击右上角分享
    */
   onShareAppMessage: function() {

   },
   // 修改get-bank
   revise(){
      //console.log(this.data.cardList)
      AJAX("bank/get-bank").then(res => {
         if (res.have_data==true){
          this.setData({
             reviseCard:false,            
          })
         }
     
      }) 
      
   },
   /**
    * bank/add-bank
    * submit
    * 提交
    */

   submit(){     
      let cardNum = this.data.yhkhdata.replace(/\s|\xA0/g, "")
      AJAX("bank/add-bank",'POST',{
         realname: this.data.cardName,
         cardno: cardNum,
         bank_id: this.data.cardVal,
         code: this.data.codeVal,
         check_key: this.data.check_key
      }).then(res => {
         if (res.status =="succeed"){
            wx.showToast({
               title: '绑定成功',
               duration:2000
            })
            wx.navigateTo({
               url: '/pages/mine/backstage/backstage',
            })
         } else{
            wx.showToast({
               title: '绑定失败',
               icon:'none',
               duration: 2000
            })
        }
      })

   },
   /**
    * 获取手机号
    */
    getPhone(){
       AJAX("member/member-info").then(res => {         
          this.setData({
             mobile:res.data.mobile
          })
       })
    },
   /**
    *  获取验证码
    */
   getCode() {
      let vm = this
      let cardLength = vm.data.yhkhdata
      
      if (cardLength == undefined){
         wx.showToast({
            title: '请输入您的银行卡账号',
            icon:'none',
            duration:2000
         })
         return
      }
    
      if (this.data.index == 0){
         wx.showToast({
            title: '请选择银行卡',
            icon: 'none',
            duration: 2000
         })
         return
      }
      if (vm.data.cardName==''){
         wx.showToast({
            title: '请输入您的名字',
            icon: 'none',
            duration: 2000
         })
         return
      }
      // 先禁止获取验证码按钮的点击
      vm.setData({
         isClick: true,
      })
      // 60s倒计时 setInterval功能用于循环，常常用于播放动画，或者时间显示
      var currentTime = vm.data.currentTime;
      interval = setInterval(function () {
         currentTime--;//减
         vm.setData({
            time: currentTime + '重新秒后获取'
         })
         if (currentTime <= 0) {
            clearInterval(interval)
            vm.setData({
               time: '获取验证码',
               currentTime: 60,
               isClick: false
            })
         }
      }, 1000);
    
    
      AJAX("sms-send/send", "POST", {
         mobile: this.data.mobile,
         tmp_code: 'common_code'
      }).then(res => {
         if (res.status == 'succeed') {
            wx.showToast({
               title: '获取成功',
               duration: 2000
            })
            this.setData({
               check_key: res.data.check_key,
              
            })
         } else {
            wx.showToast({
               title: '获取失败',
               icon: 'none',
               duration: 2000
            })
         }
      })

   },

   /**
    * bank/bank-list
    * 获取银行卡列表
    */
    getBankList(){
       AJAX("bank/bank-list").then(res => {     
        res.data.unshift(this.data.cardList);
         this.setData({
            cardList: res.data
         })
          console.log(this.data.cardList)
       }) 
    },
    /**
     * 
     * 点击选择银行
     */
   bindPickerChange: function(e) {
      console.log(e.detail.value)
      console.log(this.data.cardList)
      console.log(this.data.cardList[e.detail.value]);
      // console.log('picker发送选择改变，携带值为', e.detail.value)
      this.setData({
         reviseCard: true,
         index: e.detail.value,
         cardVal: this.data.cardList[e.detail.value].id
      })
   },

   code(e) {
      var that = this;
      console.log(e.detail.value);
      var inputValue = e.detail.value;
      if (e.detail.value.length == 6){
         that.setData({
            codeVal: inputValue,
            is_get_code:false
         })
      } else{
         that.setData({            
            is_get_code: true
         })
      }
      
   },
 
   // 跳转
   navBack() {
      wx.navigateBack()
   },
   /** 输入时候空格 */
   yhkchange(e){     
      var yhkd = e.detail.value         
    var yhkchange = yhkd.replace(/(\d{4})(?=\d)/g, "$1 ");//replace(/\s/g,'');
      this.setData({
         yhkhdata: yhkchange
      })
   },
   // 双向绑定持卡人名字
   userName(e){   
     this.setData({
        cardName: e.detail.value
     })
   }
   
})